package com.dyna.chat.event.listener;

import com.alibaba.fastjson.JSON;
import com.dyna.chat.domain.dto.msg.MsgSendMessageDTO;
import com.dyna.chat.event.MessageSendEvent;
import com.dyna.constants.TopicConstant;
import com.dyna.utils.KafkaUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 * 消息发送监听器
 *
 * @author zhongzb create on 2022/08/26
 */
@Slf4j
@Component
public class MessageSendListener {

    @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT, classes = MessageSendEvent.class, fallbackExecution = true)
    public void messageRoute(MessageSendEvent event) {
        Long msgId = event.getMsgId();
        KafkaUtil.send(TopicConstant.SEND_MSG_TOPIC, JSON.toJSONString(new MsgSendMessageDTO(msgId)));
    }

}
